<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_Standard_System extends Controller_Pc_Api_Base
{

    public function before()
    {
        parent::before();
    }

    /*
     * 获取所有系统模块类型信息列表
     */
    public function action_get_model_type_list()
    {
        $result = Model_Api_Standard_System::get_model_type_list();
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);

    }

    /**
     * 获取客服电话
     */
    public function action_get_kefu_phone()
    {
        $phone = Model_Api_Standard_System::get_sys_para('cfg_m_phone');
        $result = array(
            'phone' => $phone
        );
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);

    }

    //拥有的模块
    public function action_own_module()
    {   
        $userid = $this->request_body->content->userid;
        $siteId = Common::remove_xss($this->request_body->content->siteId);
        $secret = Common::remove_xss($this->request_body->content->secret);
        $mini_type = $this->request_body->content->mini_type; // 1 三方平台小程序(需要检查是否是组合小程序) 2 独立小程序
        $is_fenxiao = false ;
        if(St_Functions::is_normal_app_install('mobiledistribution')) {
            if (intval($userid) > 0) {
                $fenxiao = DB::select()->from('fenxiao')->where("memberid={$userid} and status=1")->execute()->current();
                if ($fenxiao && $fenxiao['status'] == 1 && $fenxiao['isfrozen'] == 0) {
                    $is_fenxiao = true;
                }
            }
        }
        // 获取权限
        $result = Model_Api_Standard_System::get_xcx_auth($mini_type,$siteId,$secret);
        // 获取产品限制
        $productTypeId = Model_Api_Standard_System::get_xcx_product_typeid($result);

        $this->send_datagrams($this->client_info['id'], array('myapplication'=>$result,'productTypeId'=> $productTypeId,'symbol'=>Currency_Tool::symbol(),'is_fenxiao'=> $is_fenxiao), $this->client_info['secret_key']);
    }

    //banner
    public function action_banner()
    {
        $result = DB::select()->from('xcx_ads')->where('prefix','=','s_index')->or_where('prefix','=','')
            ->order_by('displayorder','asc')->limit(10)->execute()->as_array();
        foreach($result as &$v){
            //判断产品类型
            if($v['typeid'] == 1)
            {
                $v['url'] = '/stpage/lines/pages/lineShow/lineShow';
                $mod = DB::select('litpic')->from('line');
            }
            elseif($v['typeid'] == 2)
            {
                $v['url'] = '/stpage/hotels/pages/hotelShow/hotelShow';
                $mod = DB::select('litpic')->from('hotel');
            }
            elseif($v['typeid'] == 3)
            {
                $v['url'] = '/stpage/cars/pages/carShow/carShow';
                $mod = DB::select('litpic')->from('car');
            }
            elseif($v['typeid'] == 5)
            {
                $v['url'] = '/stpage/spots/pages/mpShow/mpShow';
                $mod = DB::select('litpic')->from('spot');
            }
            elseif($v['typeid'] == 4)
            {
                $v['url'] = '/stpage/article/pages/show/show';
                $mod = DB::select('litpic')->from('article');
            }
            elseif($v['typeid'] == 8)
            {
                $v['url'] = '/stpage/visa/pages/visaDetail/visaDetail';
                $mod = DB::select('litpic')->from('visa');
            }
            elseif($v['typeid'] == 101)
            {
                $v['url'] = '/stpage/travelNotes/pages/details/details';
                $mod = DB::select('litpic')->from('travelNotes');
            }
            elseif($v['typeid'] == 104)
            {
                $v['url'] = '/stpage/ship/pages/show/show';
                $mod = DB::select('litpic')->from('ship_line');
            }
            elseif($v['typeid'] == 106)
            {
                $v['url'] = '/stpage/guide/pages/guideServiceDetail/guideServiceDetail';
                $mod = DB::select('litpic')->from('guide');
            }
            elseif($v['typeid'] == 114)
            {
                $v['url'] = '/stpage/outdoor/pages/details/details';
                $mod = DB::select('litpic')->from('outdoor');
            }
            elseif($v['typeid'] == 118)
            {
                $v['url'] = '/stpage/techan/pages/show/show';
                $mod = DB::select('litpic')->from('techan');
            }
            
            if(!empty($mod)){
               $row = $mod ->where('id','=',$v['proid'])->execute()->current();
               //增加图片绝对访问地址
               $v['litpic'] = Common::img($row['litpic']);
            }
        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * @function 获取分销二维码
     */
    public function action_get_code()
    {
        $xcxurl = $this->request_body->content->url;
        $pid = $this->request_body->content->id;
        $userid = $this->request_body->content->userid;

        // 获取分销配置信息
        $appid = Model_Api_Standard_System::get_fenxiao_para('cfg_wxxcx_appid');
        $secret = Model_Api_Standard_System::get_fenxiao_para('cfg_wxxcx_appsecret');

        // 将token写入缓存
        $access_token_key = 'access_token_'.$appid;
        $expire_time_key = 'token_expire_time_'.$appid;
        if(Common::cache('get',$access_token_key) && Common::cache('get',$expire_time_key)>time())
        {
            $access_token = Common::cache('get',$access_token_key);
            $access_token = json_decode($access_token,true);
        }
        else
        {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
            $access_token = Model_Api_Standard_Pub::makeRequest($url);
            Common::cache('set',$access_token_key,$access_token['result']);
            Common::cache('set',$expire_time_key,time()+7000);

            $access_token = json_decode($access_token['result'],true);
        }

        // 获取当前用户的分销码
        $userFxCode = DB::select('fxcode')->from('fenxiao')->where('memberid','=',$userid)->and_where('status','=',1)->execute()->current();
        $code = Model_Api_Standard_Pub::getWxcode($xcxurl,$pid,$userFxCode['fxcode'],$access_token);

        // 保存路径及文件名
        $path = '/uploads/xcxCode/'.date('Y').'/';
        $filename = md5($userFxCode['fxcode'].$xcxurl.$pid);
        // 创建图片并返回路径
        $imgPath = Model_Api_Standard_Pub::base64_image_content($code,$path,$filename);
        // 返回全路径
        $imgPath = Model_Api_Standard_System::reorganized_resource_url($imgPath);

        $this->send_datagrams($this->client_info['id'], $imgPath, $this->client_info['secret_key']);
    }

    /**
     * 产品获取保险
     */
    public function action_insurance()
    {
        $id = intval($this->request_body->content->id);
        $typeid = intval($this->request_body->content->typeid);
        $result = Model_Api_Standard_System::get_insurance($id,$typeid);
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * 获取常用游客信息
     */
    public function action_get_traveller()
    {
        $memberid = intval($this->request_body->content->memberid);
        $result = DB::select()->from('member_linkman')->where('memberid','=',$memberid)->execute()->as_array();
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * 添加游客信息
     */
    public function action_add_traveller()
    {
        $parms['id'] = intval($this->request_body->content->id);
        $parms['memberid'] = intval($this->request_body->content->memberid);
        $parms['linkman'] = St_Filter::remove_xss($this->request_body->content->linkman);
        $parms['en_last_name'] = St_Filter::remove_xss($this->request_body->content->en_last_name);
        $parms['en_first_name'] = St_Filter::remove_xss($this->request_body->content->en_first_name);
        $parms['cardtype'] = St_Filter::remove_xss($this->request_body->content->cardtype);
        $parms['idcard'] = St_Filter::remove_xss($this->request_body->content->idcard);
        $parms['sex'] = St_Filter::remove_xss($this->request_body->content->sex);
        $parms['nationality'] = St_Filter::remove_xss($this->request_body->content->nationality);
        $parms['cardnumber_expire_date'] = St_Filter::remove_xss($this->request_body->content->cardnumber_expire_date);
        $parms['birth'] = St_Filter::remove_xss($this->request_body->content->birth);
        $parms['mobile'] = St_Filter::remove_xss($this->request_body->content->mobile);
        if(empty($parms['id']))
        {
            unset($parms['id']);
            $result = DB::insert('member_linkman',array_keys($parms))->values(array_values($parms))->execute();
        }
        else
        {
            $result = DB::update('member_linkman')->set($parms)->where('id','=',$parms['id'])->execute();
        }

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * 删除游客信息
     */
    public function action_del_traveller()
    {
        $id = intval($this->request_body->content->id);
        $result = DB::delete('member_linkman')->where('id','=',$id)->execute();
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * 获取证件类型
     */
    public function action_get_tourer_card_type()
    {
        $result = DB::select('value')->from('sysconfig')->where('varname','=','cfg_line_tourer_cardtype')->execute()->get('value');
        $data = explode(',',$result);
        $this->send_datagrams($this->client_info['id'], $data, $this->client_info['secret_key']);
    }

    /**
     * 我的拼团
     */
    public function action_together_list()
    {
        $mid = intval($this->request_body->content->mid);
        $page = empty($this->request_body->content->page) ? 1 : intval($this->request_body->content->page);
        $pagesize = 10;
        $offset = ($page-1) * $pagesize;
        $sql = "select a.join_id,a.ordersn from sline_together_join_details a  WHERE a.memberid=$mid and a.status!=0  group by a.join_id ORDER by a.status limit $offset,$pagesize";
        $list = DB::query(1,$sql)->execute()->as_array();
        $outpage = count($list)<$pagesize ? -1 : ++$page;
        foreach ($list as &$l)
        {
            $join = DB::select()->from('together_join')->where('id','=',$l['join_id'])
                ->execute()->current();
            $l['productname'] = DB::select('productname')->from('member_order')
                ->where('ordersn','=',$l['ordersn'])->execute()->get('productname');
            $details = json_decode($join['details'],true);
            $price = array() ;
            foreach ($details as $detail)
            {
                array_push($price,$detail['price']);
            }
            $l['now_price'] = max($price);
            $l['now_number'] = Model_Together::get_join_max_number($l['join_id']);
            if($join['status']==1)
            {
                $l['lefttime'] = $join['endtime'] - time();
            }
            $l['status'] = $join['status'];
            $product = Model_Together::_get_product_info($join['typeid'],$join['productid']);
            $l['litpic'] = Common::img($product['litpic'],220,150) ;
        }
        unset($l);

        $this->send_datagrams($this->client_info['id'], $list, $this->client_info['secret_key']);
    }
}